/*
 * 文件名：LandProfitController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：huangxiaolang
 * 修改时间：2020年1月12日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.fia.web;


import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.fia.dao.LandProfitDao;
import top.infopub.mgr.fia.domain.LandProfitDomain;
import top.infopub.mgr.fia.service.LandProfitService;
import top.infopub.mgr.fia.vo.LandProfitVo;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.model.fia.LandProfit;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import com.github.pagehelper.Page;


@Controller
@RequestMapping("landProfit")
public class LandProfitController {

    @Autowired
    private LandProfitDao landProfitDao;

    @Autowired
    private LandProfitService landProfitService;

    @Autowired
    private StaffInfoService staffInfoService;

    /**
     * 列表
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "fia/lay-landProfit-list";
    }
    
    
    /**
     * 土地增减挂钩项目净利润列表数据查询
     * @return
     * @throws ParseException 
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<LandProfitDomain> queryDataByCondition(LandProfitVo landProfitVo,
                                                          ParamPage paramPage,
                                                          String startBeeDateWithoutDay,
                                                          String endBeeDateWithoutDay)
        throws ParseException {

        Page<LandProfitDomain> page = new Page<LandProfitDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (landProfitVo.getEndDate() != null) {
            landProfitVo.setEndDate(DateUtils.addDays(landProfitVo.getEndDate(), 1));
        }
        if (StringUtils.isNotBlank(startBeeDateWithoutDay)) {
            landProfitVo.setStartBeeDate(DateUtils.parseDate(startBeeDateWithoutDay, "yyyy-MM"));
        }
        if (StringUtils.isNotBlank(endBeeDateWithoutDay)) {
            landProfitVo.setEndBeeDate(DateUtils.parseDate(endBeeDateWithoutDay, "yyyy-MM"));
        }
        page = landProfitService.selectPageByDomainWhere(page, landProfitVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
    
    /**
     *获取登录部门名，登入公司名
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(HttpServletRequest request) {
        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
        request.setAttribute("bindOrgId", bindOrgId);
        request.setAttribute("bindOrgName", bindOrgName);
        request.setAttribute("bindDeptId", bindDeptId);
        request.setAttribute("bindDeptName", bindDeptName);
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        return "fia/lay-landProfit-add";
    }

    /**
     * 土地增减挂钩项目净利润-新增
     * @throws ParseException 
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(HttpServletRequest request, LandProfit landProfit, String monthWithoutDay)
        throws ParseException {
        System.err.println(monthWithoutDay);
        landProfit.setBeeDateMonth(DateUtils.parseDate(monthWithoutDay, "yyyy-MM"));
        landProfitService.saveLandProFit(landProfit);
        return RspVo.getSuccessResponse();
    }
    
    /**
     * 土地增减挂钩项目净利润-作废
     * @throws ParseException 
     */
    @RequestMapping("/cancel")
    @ResponseBody
    public Boolean cancel(HttpServletRequest request, String id) {
        Boolean flag = false;
        LandProfit superProfit = landProfitService.selectByPrimaryKey(id);
        if (superProfit.getState() == 1) {
            flag = landProfitService.changeState(id, 2);
        }
        if (superProfit.getState() == 2) {
            flag = landProfitService.changeState(id, 1);
        }
        return flag;
    }
    
    /**
     * 土地增减挂钩项目净利润-详情
     */
    @RequestMapping("/view")
    public ModelAndView details(HttpServletRequest request, String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        LandProfitDomain land = landProfitService.queryDomainById(id);
        map.put("landProfit", land);
        return new ModelAndView("fia/lay-landProfit-details", map);
    }
}
